﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace ADECMS.UserPages
{
    public partial class ProfileEdit : System.Web.UI.Page
    {
        ADECMSDBDataContext cmsdb;
        String koIme;
        protected void Page_Load(object sender, EventArgs e)
        {
            LabelIme.Text = "";
            LabelPrezime.Text = "";
            LabelUserName.Text = "";
            LabelPass.Text = "";
            LabelEmail.Text = "";
            try
            {
                cmsdb = new ADECMSDBDataContext();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            HttpCookie loginCookie = Request.Cookies["AuthCookie"];
            if (Request.IsAuthenticated)
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(loginCookie.Value);
                USER kor = cmsdb.USERs.Single(k => k.username == ticket.Name.ToString());
                koIme = kor.username;
                if (kor.USER_OPTION.avatar != null)
                {
                    Image1.ImageUrl = "AvatarHandler.ashx?uname=" + koIme;
                }
                else
                {
                    CheckBoxAvatar.Visible = false;
                    Image1.Visible = false;
                }
                if (!Page.IsPostBack)
                {
                    if (Request.IsAuthenticated)
                    {
                        Label5.Text = "Logovani ste " + ticket.Name.ToString();

                        var useri = from k in cmsdb.USERs
                                    from p in cmsdb.USER_TYPEs
                                    where k.username == ticket.Name && k.user_type_id == p.id && p.type == "Administrator"
                                    select k;
                        try
                        {
                            kor = useri.First();
                            MenuItem apanel = new MenuItem("Admin panel");
                            Menu1.Items.Add(apanel);
                        }
                        catch (System.InvalidOperationException)
                        {
                            return;
                        }
                    }
                    Ime.Text = kor.name;
                    Prezime.Text = kor.surname;
                    UserName.Text = kor.username;
                    email1.Text = kor.email;
                    Opis.Text = kor.USER_OPTION.description;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UserPages/Start.aspx");
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            if (Ime.Text.Length == 0) { LabelIme.Text = "Morate unijeti ime"; return; }
            if (Prezime.Text.Length == 0) { LabelPrezime.Text = "Morate unijeti prezime"; return; }
            if (UserName.Text.Length == 0) { LabelUserName.Text = "Morate unijeti username"; return; }
            if (Pass.Text != PassConf.Text) { LabelPass.Text = "Šifre se ne podudaraju"; return; }
            if (email1.Text.Length == 0) { LabelEmail.Text = "Morate unijeti email"; return; }

            try
            {
                USER kor = cmsdb.USERs.Single(k => k.username == koIme);
                kor.username = UserName.Text;
                LabelIme.Text = UserName.Text;
                kor.name = Ime.Text;
                kor.surname = Prezime.Text;
                if (Pass.Text.Length != 0 && PassConf.Text.Length != 0) kor.password = Pass.Text;
                kor.email = email1.Text;
                if (CheckBoxAvatar.Checked)
                {
                    kor.USER_OPTION.avatar = null;
                }
                else
                {
                    if (AvatarUpload.HasFile)
                    {
                        if (AvatarUpload.PostedFile.ContentLength > 5000000)
                        {
                            LabelAvatarUpload.Text = "Veličina slike je " + AvatarUpload.PostedFile.ContentLength.ToString();
                            return;
                        }

                        kor.USER_OPTION.avatar = AvatarUpload.FileBytes;
                    }
                }
                kor.USER_OPTION.description = Opis.Text;

                cmsdb.SubmitChanges();
                FormsAuthentication.Initialize();
                FormsAuthentication.SetAuthCookie(kor.username, false);
                Response.Redirect("/UserPages/Start.aspx");

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            switch (e.Item.Text)
            {
                case "Sign out":
                    FormsAuthentication.SignOut();
                    Response.Redirect("/Default.aspx");
                    break;
                case "Dodaj novost":
                    Response.Redirect("/UserPages/AddNews.aspx");
                    break;
                case "Pretraga":
                    Response.Redirect("/UserPages/Search.aspx");
                    break;
                case "Admin panel":
                    Response.Redirect("/UserPages/AdminPanel.aspx");
                    break;
                case "File upload":
                    Response.Redirect("/UserPages/FileUpload.aspx");
                    break;
                case "File download":
                    Response.Redirect("/UserPages/FileDownload.aspx");
                    break;
                case "Upload prezentacije":
                    Response.Redirect("/UserPages/PresentationUpload.aspx");
                    break;
                case "Pregled prezentacije":
                    Response.Redirect("/UserPages/PresentationView.aspx");
                    break;
                case "Pregledaj profil":
                    Response.Redirect("/UserPages/ProfileView.aspx");
                    break;
                case "Edituj profil":
                    Response.Redirect("/UserPages/ProfileEdit.aspx");
                    break;
            }
        }
    }
}
